﻿

<title>证书管理</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>证书管理</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-cert-formlist">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">域名类型</label>
                    <div class="layui-input-block">
                        <select name="domainNameType">
                            <option value="">不限</option>
                            <option value="1">单域名</option>
                            <option value="3">泛域名</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">域名</label>
                    <div class="layui-input-block">
                        <input type="text" name="domainName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-cert-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
            </div>

            <table id="LAY-cert-manage" lay-filter="LAY-cert-manage"></table>
            <script type="text/html" id="domainNameTypeTpl">
                {{# if(d.domainNameType==1){ }}
                <span style="color:#16b777; border: 1px solid; padding: 0px 1px;">{{ d.domainNameTypeStr }}</span>
                {{# }else if(d.domainNameType==3){ }}
                <span style="color:#1e9fff; border: 1px solid; padding: 0px 1px;">{{ d.domainNameTypeStr }}</span>
                {{# }else{ }}
                <span style="color: #16baaa; border: 1px solid; padding: 0px 1px;">{{ d.domainNameTypeStr }}</span>
                {{#  } }}
            </script>
            <script type="text/html" id="table-cert-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="view"><i class="layui-icon layui-icon-eye"></i>查看</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                {{#  if(d.canDownload == true){ }}
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="download"><i class="layui-icon layui-icon-download-circle"></i>下载证书</a>
                {{#  } else { }}
                <a class="layui-btn layui-btn-disabled layui-btn-xs"><i class="layui-icon layui-icon-download-circle"></i>下载证书</a>
                {{#  } }}
            </script>
        </div>
    </div>
</div>

<script>
    layui.use('cert', layui.factory('cert')).use(['admin', 'table'], function () {
        var $ = layui.$
            , admin = layui.admin
            , setter = layui.setter
            , view = layui.view
            , table = layui.table
            , form = layui.form;

        form.render(null, 'layadmin-cert-formlist');

        //开启搜索
        form.on('submit(LAY-cert-search)', function (data) {
            var field = data.field;

            //执行重载
            table.reload('LAY-cert-manage', {
                where: field
            });
        });

        //事件
        var active = {
            add: function () {
                var index1= admin.popup({
                    title: '添加证书'
                    , area: ['800px', '600px']
                    , id: 'LAY-popup-cert-add'
                    , success: function (layero, index) {
                        view(this.id).render('cert/addcertmanageform').done(function () {
                            form.render(null, 'layuiadmin-form-addcertmanage-adddomainname');
                            form.render(null, 'layuiadmin-form-addcertmanage-verifydomainname');

                            //开启提交
                            form.on('submit(LAY-cert-applycert-front-submit)', function (data) {
                                var field = data.field; //获取提交的字段

                                var formAddCertManageData = form.val('layuiadmin-form-addcertmanage-adddomainname');
                                var formVerifyDomainNameData = form.val('layuiadmin-form-addcertmanage-verifydomainname');
                                var newData = {
                                    domainNameType: formAddCertManageData.domainNameType,
                                    domainName: formAddCertManageData.domainName,
                                    dnsAuthId: formVerifyDomainNameData.dnsAuth
                                };
                                var index2= layer.confirm('确定申请证书吗？', function (index) {
                                    $.ajax({
                                        url: setter.baseConfig.apiHost + '/api/certificate/add'
                                        , type: 'post'
                                        , contentType: "application/json"
                                        , data: JSON.stringify(newData)
                                        , beforeSend: function (xhr) {
                                            xhr.setRequestHeader("Authorization", 'Bearer ' + layui.data(setter.tableName)[setter.request.tokenName]);
                                        }
                                        , success: function (res) {
                                            if (res.errorCode != '00000') {
                                                layer.msg(res.errorMessage, {
                                                    offset: '15px'
                                                    , icon: 2
                                                    , time: 3000
                                                });
                                                return false;
                                            }

                                            //登入成功的提示与跳转
                                            layer.msg('申请成功', {
                                                offset: '15px'
                                                , icon: 1
                                                , time: 3000
                                            });

                                            layui.table.reload('LAY-cert-manage'); //重载表格
                                            layer.close(index2); //执行关闭
                                            layer.close(index1);
                                        }
                                    });
                                });

                                return false;
                            });
                        });
                    }
                });

                //最大化
                layer.full(index1);
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>